<?php
namespace app\admin\controller;
use think\Db;

use app\admin\controller\Common;
use app\service\roominfoService;
use app\common\controller\Sendmsg;
class Statis extends Common{
    public function day(){
         //查询小区信息
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $communitylist = (new roominfoService())->communityList($whe);
        $community_id = $com['community_id'];
        $this->assign('communitylist',$communitylist);
        $data = request()->get();
        $where=array();
        if (!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
        }
        $where = array_filter($where);
        $list=Db::table("statis")->where("community_id","in",$community_id)->where($where)->select();
        $number=Db::table("statis")->where("community_id","in",$community_id)->where($where)->count();
        $this->assign("list",$list);
        $this->assign("number",$number);
         foreach ($data as $k => $v) {
            $this->assign($k, $v);
        }
        return $this->fetch();
    }
    
    public function month(){
        //查询小区信息
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $communitylist = (new roominfoService())->communityList($whe);
        $community_id = $com['community_id'];
        $this->assign('communitylist',$communitylist);
        $data = request()->get();
        $where=array();
        if (!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
        }
        $where = array_filter($where);
        $list=Db::table("statis_month")->where("community_id","in",$community_id)->where($where)->select();
        $number=Db::table("statis_month")->where("community_id","in",$community_id)->where($where)->count();
        $this->assign("list",$list);
        $this->assign("number",$number);
         foreach ($data as $k => $v) {
            $this->assign($k, $v);
        }
        return $this->fetch();
    }
    
    //日报设置，手动推送短信
    public function smssend(){
        
          //查询小区信息
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $communitylist = (new roominfoService())->communityList($whe);
        $community_id = $com['community_id'];
        $this->assign('communitylist',$communitylist);
        
        if(request()->isPost()){
            $data=request()->post();
            $communityinfo=Db::table("community")->where("community_id",$data["community_id"])->find();
            $con="【".$communityinfo["community_name"]."】"."当日收款总金额：".$data["money"]."元，支付宝：".$data["alipay"]."元，微信：".$data["wechat"]."元，现金：".$data["cash"]."元，POS机".$data["POS"]."元，当天缴费户数：".$data["roomnumber"]."户，缴费率完成：35%";
            //查询该房屋24小时之内是否发送信息
            $smsinfo=Db::table("smslogcom")
                        ->where("phone",$data["phone"])
                        ->where("create_time",'>',time()-24*3600)
                        ->find();
            if(empty($smsinfo)){
                $res=(new Sendmsg())->sendmsg($data["phone"],$con);
                if($res=="100"){
                    $data["community_name"]=$communityinfo["community_name"];
                    $data["create_time"]=time();
                    $data["operator"]=session("name");
                    Db::table("smslogcom")->insert($data);
                    $this->success("短信发送成功",url('smsloglist'));
                }else{
                    $this->error("短信发送失败，请重新发送");
                    return "短信发送失败，请重新发送";
                }
            }else{
                $this->error("24小时内不能重复发送信息");
                return "24小时内不能重复发送信息";
            }
        }else{
            return $this->fetch();
        }
        
    }
    
    public function smsloglist(){
         //查询小区信息
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $communitylist = (new roominfoService())->communityList($whe);
        $community_id = $com['community_id'];
        $this->assign('communitylist',$communitylist);
        $data = request()->get();
        $where=array();
        if (!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
        }
        $where = array_filter($where);
        $list=Db::table("smslogcom")->where("community_id","in",$community_id)->where($where)->select();
        $number=Db::table("smslogcom")->where("community_id","in",$community_id)->where($where)->count();
        $this->assign("list",$list);
        $this->assign("number",$number);
         foreach ($data as $k => $v) {
            $this->assign($k, $v);
        }
        return $this->fetch();
    }
}